package com.dc.di.util;

import java.util.Date;

import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;

import com.dc.adaptor.persistance.ConnectionCallback;
import com.dc.adaptor.persistance.Const;
import com.dc.adaptor.persistance.DatabaseHandler;

public class FaultInfoRecord extends DatabaseHandler{

	private static FaultInfoRecord instance;

	public static FaultInfoRecord getInstance() {
		if (instance == null) {
			instance = new FaultInfoRecord();
		}
		return instance;
	}

	public void recordFaultlog(final String exName,final String exDescription) {
		try{
			doInConnection(new ConnectionCallback<String>(){
	
				@Override
				public String doInConnection(Database db) throws Exception {
					Object[] rowData  = new Object[]{
							GUIDGen.getGUID(),
							exName,
							exDescription,
							new Date(),
							"0"
					};
					RowMetaInterface rowMeta = getFaultFields();
					insert(Const.TABLE_FAULT,rowMeta,rowData);	
					if(!db.isAutoCommit()){
						db.commit();
					}
					return null;
				}}
			);
		}catch (Exception e) {
			logger.error("save faultinfo error", e);
		}
	}
	
	public RowMetaInterface getFaultFields() throws KettleDatabaseException {
		RowMetaInterface metaData = new RowMeta();
		ValueMetaInterface v;
		v = new ValueMeta("FI_NM", ValueMetaInterface.TYPE_STRING, 32, 0);
		metaData.addValueMeta(v);
		v = new ValueMeta("FI_YCMC", ValueMetaInterface.TYPE_STRING, 32, 0);
		metaData.addValueMeta(v);
		v = new ValueMeta("FI_YCMS", ValueMetaInterface.TYPE_STRING, 1000, 0);
		metaData.addValueMeta(v);
		v = new ValueMeta("FI_FSSJ", ValueMetaInterface.TYPE_DATE);
		metaData.addValueMeta(v);
		v = new ValueMeta("FI_TBZT", ValueMetaInterface.TYPE_STRING,1,0);
		metaData.addValueMeta(v);
		return metaData;
	}
}
